from manim import *
class math(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{(2019年)求曲线}$y=e^{-x}\sin x ( x\geq  0) $\text{与X轴之间的面积}"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        
        self.play(Write(text),self.camera.frame.animate.scale(0.8))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text.animate().move_to([0,20,0]) )
        rectangle=Rectangle(color=BLUE,fill_opacity=0.3,width=70,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([0, 20, 0])
        self.play(Create(rectangle))
        
        text1 = Tex(r"\text{面积S}$=\int _{a}^{b}\left| f\left( x\right) \right| dx $"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,12,0])
        
        self.play(Write(text1))

        text2 = Tex(r"$S=\int _{0}^{+\infty }\left| e^{-x}\sin x\right| dx$"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,12,0])
        
        self.play(ReplacementTransform(text1,text2))
        self.wait(0.2)
        rectangle1=Rectangle(color=GREEN,fill_opacity=0.3,width=30,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle1.move_to([0, 12, 0])
        self.play(Create(rectangle1))
        self.play(FadeOut(rectangle1))
        
        #下划线
        dot1=Dot([-1,10,0])
        dot2=Dot([9,10,0])
        line=Line(dot1,dot2).set_color(YELLOW)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))
        text3 = Tex(r"$S=\int _{0}^{+\infty } e^{-x}\left|\sin x\right| dx$"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,12,0])
        
        self.play(ReplacementTransform(text2,text3),FadeOut(line))
        self.wait(0.2)

        rectangle2=Rectangle(color=GREEN,fill_opacity=0.3,width=4.5,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle2.move_to([0, 12, 0])
        rectangle3=Rectangle(color=GREEN,fill_opacity=0.3,width=4.5,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle3.move_to([0, 12, 0])
        self.play(Create(rectangle2),Create(rectangle3))
        text4 = Tex(r"\text{加减换元}",color=YELLOW
                  
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        
        self.play(ReplacementTransform(rectangle3,text4))
        self.wait(0.2)
        text5 = Tex(r"\text{无限区间}",color=YELLOW
                  
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,0,0])
        text6 = Tex(r"\text{有限区间}",color=YELLOW
                  
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,0,0])
        arrow = Arrow(start=[-5,0,0],end=[5,0,0],color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Write(text5),Write(text6),GrowArrow(arrow))
        text7 = Tex(r"\text{记为I}",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text3,RIGHT,buff=3)
        self.play(FadeOut(text5),FadeOut(text6),FadeOut(arrow),FadeOut(text4),Write(text7))
        text8 = Tex(r"\text{令}$x=t-\pi $",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        self.play(Write(text8))
        text9 = Tex(r"$\int _{\pi }^{+\infty }e^{\pi -t}\left| \sin \left( t-\pi \right) \right| dt$"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        
        self.play(ReplacementTransform(text8,text9))
        self.wait(0.2)
        text10 = Tex(r"$e^{\pi }\cdot \int _{\pi }^{+\infty }e^{-t}\left| \sin t\right| dt$"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        
        self.play(ReplacementTransform(text9,text10))
        self.wait(0.2)
        text11 = Tex(r"$e^{\pi }\cdot \left[ \int _{0}^{+\infty }e^{-t}\left| \sin t\right| dt-\int _{0}^{\pi }e^{-t}\left| \sin t\right| dt\right] $"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-1,0])
        
        self.play(Write(text11))
        self.wait(0.2)
        rectangle3=Rectangle(color=GREEN,fill_opacity=0.3,width=19,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle3.move_to([-7.5, -1, 0])
        self.play(Create(rectangle3))
        rectangle2=Rectangle(color=RED,fill_opacity=0.0,width=45,height=6
                            ,stroke_color=RED,stroke_width=20)
        rectangle2.move_to([0, -1, 0])
        self.play(Create(rectangle2))
        text12 = Tex(r"$I=e^{\pi }\cdot \left[ I-\int _{0}^{\pi }e^{-t}\left| \sin t\right| dt\right]  $"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-7,0])
        self.play(Write(text12))
        text13 = Tex(r"$I=\dfrac{e^{\pi }}{e^{\pi }-1}\cdot \int _{0}^{\pi }e^{-t}\left| \sin t\right| dt$"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-7,0])
        
        self.play(ReplacementTransform(text12,text13))
        self.wait(0.2)
        
        #下划线
        dot1=Dot([2,-10,0])
        dot2=Dot([13,-10,0])
        line=Line(dot1,dot2).set_color(GREEN)
        line.set_stroke(width=30)  # 设置宽度为 30
        
        text14 = Tex(r"$0\leq x\leq \pi ,$\text{时}$\sin x\geq 0$",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-15,0])
        self.play(Write(text14),Create(line),FadeOut(rectangle2),FadeOut(rectangle3))
        text15 = Tex(r"$I=\dfrac{e^{\pi }}{e^{\pi }-1}\cdot \int _{0}^{\pi }e^{-t}\sin tdt$"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-7,0])
        
        self.play(ReplacementTransform(text14,text15),FadeOut(line),ReplacementTransform(text13,text15))
        self.wait(0.2)
        rectangle4=Rectangle(color=RED,fill_opacity=0.3,width=16,height=6
                            ,stroke_color=RED,stroke_width=20)
        rectangle4.move_to([7.5, -7, 0])
        self.play(Create(rectangle4))
        text16 = Tex(r"$\int e^{-t}\sin tdt=\dfrac{-\left( \sin t+\cos t\right) }{2}\cdot e^{-t} +C$"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([7.5,-15,0])
        self.play(Write(text16))
        text17 = Tex(r"\text{带入}$ \int _{0}^{\pi }$",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,-20,0])
        self.play(Create(text17))
        text18 = Tex(r"$\int _{0}^{\pi }e^{-t}\sin tdt=\dfrac{1}{2}\left( e^{-\pi }+1\right)$",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([7.5,-15,0])
        
        self.play(ReplacementTransform(text16,text18),ReplacementTransform(text17,text18))
        self.wait(0.2)
        text19 = Tex(r"$=\dfrac{1}{2}\cdot \dfrac{e^{\pi }+1}{e^{\pi }-1}$",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text15,RIGHT,buff=1)
        
        self.play(ReplacementTransform(text18,text19))
        self.wait(0.2)
        self.wait(1)